Your company delivers breakfast via autonomous quadcopter drones. And something mysterious has happened.
Each breakfast delivery is assigned a unique ID, a positive integer. When one of the company's 100 drones takes off with a delivery, the delivery's ID is added to a list, delivery_id_confirmations. When the drone comes back and lands, the ID is again added to the same list.
After breakfast this morning there were only 99 drones on the tarmac. One of the drones never made it back from a delivery. We suspect a secret agent from Amazon placed an order and stole one of our patented drones. To track them down, we need to find their delivery ID.
Given the list of IDs, which contains many duplicate integers and one unique integer, find the unique integer.
The IDs are not guaranteed to be sorted or sequential. Orders aren't always fulfilled in the order they were received, and some deliveries get cancelled before takeoff.
We XOR all the integers in the list. We start with a variable unique_delivery_id set to . Every time we XOR with a new ID, it will change the bits. When we XOR with the same ID again, it will cancel out the earlier change.
In the end, we'll be left with the ID that appeared once!
def find_unique_delivery_id(delivery_ids):
unique_delivery_id = 0
for delivery_id in delivery_ids:
unique_delivery_id ^= delivery_id
return unique_delivery_id
time, and space.
This problem is a useful reminder of the power we can unlock by knowing what's happening at the "bit level."
How do you know when bit manipulation might be the key to solving a problem? Here are some signs to watch out for:
Do you have an answer?
Wanna review this one again later? Or do you feel like you got it all?
Mark as done Pin for review laterReset editor
Powered by qualified.io